@extends('layouts.app')
@section('title','编辑帖子')
@section('editor_css')
    <link rel="stylesheet" type="text/css" href="{{ asset('css/simditor.css') }}">
@stop

@section('content')
    <div class="row">
        <div class="offset-md-2 col-md-8">
            <div class="card">
                <div class="card-header">
                    <h3 class="pt-2 text-center">
                        <i class="fa-solid fa-pencil"></i>
                        编辑帖子
                    </h3>
                </div>
                <div class="card-body">
                    <form action="{{route('topics.update',$topic->id)}}" method="POST" accept-charset="UTF-8">
                        @csrf
                        {{method_field('PUT')}}
                        @include('shared._errors')
                        <div class="mb-3">
                            <label for="title" class="form-label">帖子标题</label>
                            <input type="text" name="title" class="form-control" id="title"
                                   placeholder="请输入帖子标题"
                                   value="{{old('title',$topic->title)}}">
                        </div>
                        <div class="mb-3">
                            <label for="category_id" class="form-label">所属分类</label>
                            <select class="form-select" id="category_id" name="category_id" aria-label="请选择...">
                                <option value="">请选择...</option>
                                @foreach($categories as $category)
                                    <option value="{{$category->id}}"
                                            {{$topic->category_id == $category->id ? 'selected' : ''}}
                                    >{{$category->name}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-3">
                            <label for="content" class="form-label">帖子内容</label>
                            <textarea id="editor" class="form-control"
                                      name="content" id="content" rows="3">{{old('content',$topic->content)}}</textarea>
                        </div>
                        <hr>
                        <div class="text-center">
                            <button type="submit" class="btn btn-primary">修改</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@stop
@section('editor_js')
    <script type="text/javascript" src="{{ asset('js/module.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/hotkeys.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/uploader.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/simditor.js') }}"></script>

    <script>
        $(document).ready(function() {
            let editor = new Simditor({
                textarea: $('#editor'),
                upload:{
                    url:'{{route('topics.upload_image')}}',
                    params:{
                        _token:'{{csrf_token()}}'
                    },
                    fileKey:'upload_file',
                    connectionCount:5,
                    leaveConfirm:'图片正在上传中，离开会取消上传'
                },
                pasteImage:true
            });
        });
    </script>
@stop
